unit UnitRegiao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UnitModelo, DB, ImgList, ActnList, ADODB, ExtCtrls,
  ComCtrls, ToolWin, Grids, DBGrids, SMDBGrid, StdCtrls, Mask, DBCtrls,
  jpeg;

type
  TFormRegiao = class(TFormModelo)
    qryUsuario: TADOQuery;
    dsUsuario: TDataSource;
    qryUsuarioUSUARIO_LOGIN: TStringField;
    qryUsuarioUSUARIO_SENHA: TVarBytesField;
    qryUsuarioPERFIL_CODIGO: TWordField;
    qryUsuarioUSUARIO_NOME: TStringField;
    qryUsuarioUSUARIO_EMAIL: TStringField;
    qryDadosREGIAO_DESCRICAO: TStringField;
    qryDadosREGIAO_CODIGO: TWordField;
    qryDadosUSUARIO_LOGIN: TStringField;
    TabSheet1: TTabSheet;
    SMDBGrid1: TSMDBGrid;
    qryFranGrupo: TADOQuery;
    dsFranGrupo: TDataSource;
    qryFranGrupoFRANQUIA_CODIGO: TIntegerField;
    qryFranGrupoREGIAO: TWordField;
    qryFranGrupoREGIAO_CODIGO: TWordField;
    Label1: TLabel;
    Label2: TLabel;
    DBText2: TDBText;
    DBText3: TDBText;
    Label3: TLabel;
    qryFranquia: TADOQuery;
    qryFranquiaCODIGO: TIntegerField;
    qryFranquiaNOME_FANTASIA: TStringField;
    qryFranquiaREGIAO: TWordField;
    qryFranGrupoFranquia: TStringField;
    qryDadosUsuario: TStringField;
    DBText1: TDBText;
    procedure qryFranGrupoAfterInsert(DataSet: TDataSet);
    procedure actNovoExecute(Sender: TObject);
    procedure actAlterarExecute(Sender: TObject);
    procedure qryDadosAfterOpen(DataSet: TDataSet);
    procedure qryDadosBeforeClose(DataSet: TDataSet);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure qryDadosBeforeOpen(DataSet: TDataSet);
    procedure qryDadosAfterClose(DataSet: TDataSet);
    procedure qryFranGrupoBeforeOpen(DataSet: TDataSet);
    procedure qryFranGrupoAfterCancel(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormRegiao: TFormRegiao;

implementation

uses UnitPrincipal, UnitCadregiao;

{$R *.dfm}

procedure TFormRegiao.qryFranGrupoAfterInsert(DataSet: TDataSet);
begin
  inherited;
  if qryDados.State = dsinsert then
    qryDados.Post;
  if not(qryFranGrupo.State in [dsinsert,dsEdit]) then
    qryFranGrupo.Edit;
  qryFranGrupoREGIAO_CODIGO.Value := qryDadosREGIAO_CODIGO.Value;
end;

procedure TFormRegiao.actNovoExecute(Sender: TObject);
begin
  inherited;
  FormPrincipal.cnRegente.BeginTrans;
  qryGeral.SQL.Text := 'SELECT ISNULL(MAX(REGIAO_CODIGO),0)+1 FROM REGIAO';
  qryGeral.Open;
  qryDados.Insert;
  qryDadosREGIAO_CODIGO.value := qryGeral.Fields.Fields[0].Value;
  if FormCadRegiao = nil then
    FormCadRegiao := TFormCadRegiao.Create(Self);
  if FormCadRegiao.ShowModal = mrOk then
    FormPrincipal.cnRegente.CommitTrans
  else
  begin
    FormPrincipal.cnRegente.RollbackTrans;
    qryDados.Refresh;
  end;
end;

procedure TFormRegiao.actAlterarExecute(Sender: TObject);
begin
  inherited;
  FormPrincipal.cnRegente.BeginTrans;
  qryDados.Edit;
  if FormCadRegiao = nil then
    FormCadRegiao := TFormCadRegiao.Create(Self);
  if FormCadRegiao.ShowModal = mrOk then
    FormPrincipal.cnRegente.CommitTrans
  else
  begin
    FormPrincipal.cnRegente.RollbackTrans;
    qryDados.Refresh;
  end;
end;

procedure TFormRegiao.qryDadosAfterOpen(DataSet: TDataSet);
begin
  inherited;
  qryFranGrupo.Open
end;

procedure TFormRegiao.qryDadosBeforeClose(DataSet: TDataSet);
begin
  inherited;
  qryFranGrupo.Close;
end;

procedure TFormRegiao.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  FormRegiao := nil;
end;

procedure TFormRegiao.qryDadosBeforeOpen(DataSet: TDataSet);
begin
  inherited;
  qryUsuario.Open;
end;

procedure TFormRegiao.qryDadosAfterClose(DataSet: TDataSet);
begin
  inherited;
  qryUsuario.Close;
end;

procedure TFormRegiao.qryFranGrupoBeforeOpen(DataSet: TDataSet);
begin
  inherited;
  qryFranquia.Open;
end;

procedure TFormRegiao.qryFranGrupoAfterCancel(DataSet: TDataSet);
begin
  inherited;
  qryFranquia.Cancel;
end;

end.
